package com.ulic.customerManApplication.messagemodule.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.ulic.customerManApplication.base.BaseAction;
import com.ulic.customerManApplication.commonmodule.bo.BirthdayMessage;
import com.ulic.customerManApplication.commonmodule.bo.Organization;
import com.ulic.customerManApplication.commonmodule.vo.ImpDataInfor;
import com.ulic.customerManApplication.constant.CONSTANT;
import com.ulic.customerManApplication.membermodule.bo.Member;
import com.ulic.customerManApplication.membermodule.service.MemberService;
import com.ulic.customerManApplication.membermodule.vo.MemberVO;
import com.ulic.customerManApplication.messagemodule.bo.MessageRecord;
import com.ulic.customerManApplication.messagemodule.service.MessageService;
import com.ulic.customerManApplication.messagemodule.vo.MessageRecordVO;
import com.ulic.customerManApplication.pager.PagerModel;
import com.ulic.customerManApplication.usermodule.bo.User;
import com.ulic.customerManApplication.util.message.MessageUtil;

@Component("messageAction")
@Scope("prototype")
public class MessageAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private MessageService messageService;
	private MessageRecord messageRecord;
	private BirthdayMessage birthdayMessage;
	private String impDataInfor;
	private MemberService memberService;
	private String receive;
	private String receiveId;
	
	public String getImpDataInfor() {
		return impDataInfor;
	}

	public void setImpDataInfor(String impDataInfor) {
		this.impDataInfor = impDataInfor;
	}

	public String getReceiveId() {
		return receiveId;
	}

	public void setReceiveId(String receiveId) {
		this.receiveId = receiveId;
	}

	public String getReceive() {
		return receive;
	}

	public void setReceive(String receive) {
		this.receive = receive;
	}

	public BirthdayMessage getBirthdayMessage() {
		return birthdayMessage;
	}

	public void setBirthdayMessage(BirthdayMessage birthdayMessage) {
		this.birthdayMessage = birthdayMessage;
	}

	public MessageRecord getMessageRecord() {
		return messageRecord;
	}

	public void setMessageRecord(MessageRecord messageRecord) {
		this.messageRecord = messageRecord;
	}

	public MessageService getMessageService() {
		return messageService;
	}

	@Resource
	public void setMessageService(MessageService messageService) {
		this.messageService = messageService;
	}
	public MemberService getMemberService() {
		return memberService;
	}
    @Resource
	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		HttpServletRequest httpServletRequest = ServletActionContext
				.getRequest();
		PagerModel pm = messageService.getMessageRecords(curPage);
		httpServletRequest.setAttribute("pm", pm);
		return SUCCESS;
	}

	public String searchMessageRecords() throws Exception {
		HttpServletRequest httpServletRequest = ServletActionContext
				.getRequest();
		String startDate = httpServletRequest.getParameter("startDate");
		String endDate = httpServletRequest.getParameter("endDate");
		String roleName = (String) session.get("rolename");
		User user = (User) session.get(CONSTANT.USER_SESSION_KEY);
		MessageRecordVO messageRecordVO = new MessageRecordVO();
		messageRecordVO.setStartDate(startDate);
		messageRecordVO.setEndDate(endDate);
		messageRecordVO.setUser(user);
		messageRecordVO.setRoleName(roleName);
		messageRecordVO.setMessageRecord(messageRecord);
		PagerModel pm = messageService.getMessageRecords(messageRecordVO,
				curPage);
		httpServletRequest.setAttribute("pm", pm);
		httpServletRequest.setAttribute("startDate", startDate);
		httpServletRequest.setAttribute("endDate", endDate);
		return SUCCESS;
	}

	public String saveBirthdayBless() {
		User user = (User) session.get(CONSTANT.USER_SESSION_KEY);
		birthdayMessage.setUser(user);
		messageService.addBirthdayBless(birthdayMessage);
		return SUCCESS;
	}

	public String showBirthdayMessage() {
		User user = (User)session.get(CONSTANT.USER_SESSION_KEY);
		birthdayMessage = messageService.getBirthdayMessageByType(user,"MESSAGE");
		return SUCCESS;
	}

	public String deleteMessageRecord() {
		messageRecord = messageService.getMessageRecordById(messageRecord.getMessage_id());
		messageService.removeMessageRecord(messageRecord);
		return SUCCESS;
	}

	public String viewMessageRecord() {
		HttpServletRequest httpServletRequest = ServletActionContext
		.getRequest();
		messageRecord = messageService.getMessageRecordById(messageRecord.getMessage_id());
	    String receives = "";
		Set<Member> members = messageRecord.getReceiver();
	    for(Member member : members){
	    	receives = receives+" "+member.getMemberName();
	    }
	    httpServletRequest.setAttribute("receives", receives);
		return SUCCESS;
	}

	public String toSendMessage() {
		HttpServletRequest httpServletRequest = ServletActionContext
				.getRequest();
		MemberVO memberVO = new MemberVO();
		String roleName = (String)session.get("rolename");
		User user = (User)session.get(CONSTANT.USER_SESSION_KEY);
		memberVO.setRoleName(roleName);
		Member	member = new Member();
		if("SUBADMIN".equals(roleName)){
			 Organization organ = (Organization)session.get("barchList");
			 if(organ!=null){
				 member.setSubCompany(organ.getUm_organ_id());
			 }
		}else if("THIRDADMIN".equals(roleName)){
			 Organization subComOrgan = (Organization)session.get("barchList");
			 if(subComOrgan!=null){
				 member.setSubCompany(subComOrgan.getUm_organ_id());
			 }
			Organization organ = (Organization)session.get("expensedList");
			 if(organ!=null){
				 member.setSubCity(organ.getUm_organ_id());
			 }
		} 
		memberVO.setMember(member);
		memberVO.setUser(user);
		PagerModel pm = memberService.getMembers(memberVO, curPage);
		httpServletRequest.setAttribute("members",memberService.getMembers(memberVO, pm.getTotal()).getDatas());
		httpServletRequest.setAttribute("error",httpServletRequest.getParameter("error"));
		return SUCCESS;
	}
    /**
     * 发送短信
     * @return
     * @throws Exception
     */
	public String sendMessage()throws Exception {
		User user = (User) session.get(CONSTANT.USER_SESSION_KEY);
		String[] receiveMemberIds = receiveId.split(",");
		Member member = null;
		Set<Member> members = new HashSet<Member>();
		List<String> cellPhones = new ArrayList<String>();//发送短信的手机号集合
		for(String receiveMemberId : receiveMemberIds){
			member = memberService.getMember(Integer.parseInt(receiveMemberId.trim()));
			members.add(member);
			String cellPhone = member.getCellPhone();
			if(cellPhone != null && cellPhone.length()!=0){
				cellPhones.add(cellPhone);
			}
		}
		Date now = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String sendTime = sdf.format(now);
		messageRecord.setSender(user);
		messageRecord.setSendTime(sendTime);
		MessageUtil messageUtil = new MessageUtil();
		List<String> succeedCellPhone = new ArrayList<String>();
		for(String cellphone : cellPhones){
			String result = messageUtil.directSendMessage(cellphone, messageRecord.getMessageContent(), "130001");
			if("Y".equals(result)){
				succeedCellPhone.add(cellphone);
			}
		}
		ImpDataInfor impDataInfor = new ImpDataInfor();
		impDataInfor.setTotalNum(cellPhones.size());
		impDataInfor.setSucceedNum(succeedCellPhone.size());
		impDataInfor.setFailNum(cellPhones.size()-succeedCellPhone.size());
		this.setImpDataInfor(impDataInfor.toString());
		Set<Member> succeedSends = new HashSet<Member>();
		for(Member m : members){
			if(succeedCellPhone.contains(m.getCellPhone())){
				succeedSends.add(m);
			}
		}
		if(succeedSends.size()!=0){
			messageRecord.setReceiver(succeedSends);
			messageService.addMessageRecords(messageRecord);
		}
		return SUCCESS;
		
	}
}
